<?php

/**
 * testcase actions.
 *
 * @package    tectesu
 * @subpackage testcase
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class testcaseActions extends sfActions
{
  public function createComboUser()
  {
    
  }
  public function executeSearch()
  {
  	
  }
  
  public function executeFinder(sfWebRequest $request)
  {
  	$this->testCase = new Testcase();
  	
  	$this->idExists = $this->testCase->getExistId($request->getParameter('search'));

  	if ($this->idExists)
  	{
     $this->redirect('testcase/edit?id='.$request->getParameter('search'));
  	}
  	
  	return $this->forward('testcase', 'secure');
  	    	      
  }
	
  public function executeIndex(sfWebRequest $request)
  {   
     return $this->forward('testcase', 'signin');
  }
  
  /**
    * This function shows in a list all the the posts.
    */
  public function executeList(sfWebRequest $request)
  {
    $this->TestCase = new Testcase();
   	
    $this->testcase_list = $this->Testcase()->getAllTestCase();   	
  }
   
  /**
    * This function send to a signinSuccess.php.class, this template show a fields where you must enter login and password.
    */
  public function executeSignin()
  {
    sfView::SUCCESS;
  }
  
  /**
    * Check if name and password exists on the database
    */
  public function executeCheck(sfWebRequest $request)
  {	
   $this->testCase = new Testcase();
  	
   $this->IdByNamePass = $this->testCase->getCheckNamePassword($request->getParameter('username'), $request->getParameter('password'));
   
   if ($this->IdByNamePass[0])
   {
     //$this->urlValues = $this->IdByNamePass[1].'&username='.$this->IdByNamePass[2].'&password='.$this->IdByNamePass[3];
     //$this->redirect('testcase/ListTestcaseById?id=2');//.$this->urlValues);    
     $this->redirect('testcase/ListTestcaseById?id='.$this->IdByNamePass);
   }
   else
   {
     return $this->forward('testcase', 'secure');
   }
   //sfView::NONE;
  }
  
  /**
    * Redirect to a template call secure, and show that your name and password are incorrect.
    */       
  public function executeSecure()
  {
  
  }	

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new TestcaseForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new TestcaseForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($testcase = Doctrine::getTable('Testcase')->find(array($request->getParameter('id'))), sprintf('Object testcase does not exist (%s).', array($request->getParameter('id'))));

    $this->form = new TestcaseForm($testcase);
  }

  public function executeListTestcaseById(sfWebRequest $request)
  {
    $this->id = NULL;
    //$username= $request->getParameter('usernamet');
    //$password=  $request->getParameter('password');
    $this->id = $request->getParameter('id');
    if (!$this->id)
    {
      $this->id = $this->getUser()->getAttribute('IdUser');
    }
    
    $this->testCase = new Testcase();
    
    if ($this->id)
    {
      $this->objUser = $this->testCase->getUserById($this->id);
      $this->getUser()->setAuthenticated(true);
      $this->getUser()->setAttribute('username', $this->objUser[0]->getusername());
      $this->getUser()->setAttribute('password', $this->objUser[0]->getpassword());
      $this->getUser()->setAttribute('IdUser', $this->objUser[0]->getid());
    }  
    
    $this->testcase_list = $this->testCase->getListTestcaseById($this->id);
        
  }
  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    
    $this->forward404Unless($testcase = Doctrine::getTable('Testcase')->find(array($request->getParameter('id'))), sprintf('Object testcase does not exist (%s).', array($request->getParameter('id'))));
    
    $this->form = new TestcaseForm($testcase);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($testcase = Doctrine::getTable('Testcase')->find(array($request->getParameter('id'))), sprintf('Object testcase does not exist (%s).', array($request->getParameter('id'))));
    
    $testcase->delete();

    $this->redirect('testcase/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    
    if ($form->isValid())
    {
      $testcase = $form->save();

      $this->redirect('testcase/edit?id='.$testcase['id']);
    }
  }
  
  public function executeListComponent(sfWebRequest $request)
  {
    $this->component = new Component();
    
    $this->component_list = $this->component->getListComponentById($request->getParameter('id'));
  }
  
  public function executeTestCaseById(sfWebRequest $request)
  {
    $this->testCase = new Testcase();
    
    $this->testcaseListById = $this->testCase->getListTestcaseById($request->getParameter('id'));
  }
  
  public function executeListTestSuite(sfWebRequest $request)
  {
    $this->TestSuite = new Testsuite();
    
    $this->testsuite_list = $this->TestSuite->getListTestSuiteById($request->getParameter('id')); 
  }
  
}
